package com.jike.shop.common.message;

import com.jike.shop.common.enums.ReplyEnum;
import com.jike.shop.common.enums.ResultCodeEnum;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Data
public class Replys<T> {
    private boolean success;
    private String resultCode;
    private String resultMessage;
    private T resultData;

    private Replys(){

    }

    private Replys(boolean success, String resultCode, String resultMessage, T resultData){
        this.success = success;
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;
        this.resultData = resultData;
    }

    private Replys(boolean success, ReplyEnum resultCodeEnum, T resultData){
        this.success = success;
        this.resultCode = resultCodeEnum.getCode();
        this.resultMessage = resultCodeEnum.getMessage();
        this.resultData = resultData;
    }

    public static <T> ResponseEntity<Replys<T>> ok(T resultData){
        return ResponseEntity.status(HttpStatus.OK)
                .body(new Replys<>(true, ResultCodeEnum.SUCCESS, resultData));
    }

    public static <T> ResponseEntity<Replys<T>> ok(String code, String massage, T resultData) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(new Replys<>(true, code, massage, resultData));
    }

    public static <T> ResponseEntity<Replys<T>> fail(boolean success, ReplyEnum resultCodeEnum) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(new Replys<>(success, resultCodeEnum, null));
    }

    public static <T> ResponseEntity<Replys<T>> fail(String code, String massage) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(new Replys<>(true, code, massage, null));
    }

    public static <T> ResponseEntity<Replys<T>> fail(boolean success, ReplyEnum resultCodeEnum, T resultData) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(new Replys<>(success, resultCodeEnum, resultData));
    }

    public static <T> ResponseEntity<Replys<T>> error(boolean success, T resultData) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new Replys<>(success, ResultCodeEnum.ERROR, resultData));
    }

    public static <T> ResponseEntity<Replys<T>> error(boolean success, ReplyEnum resultCodeEnum) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new Replys<>(success, resultCodeEnum, null));
    }

    public static <T> ResponseEntity<Replys<T>> error(boolean success, ReplyEnum resultCodeEnum,T resultData) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new Replys<>(success, resultCodeEnum, resultData));
    }
}
